/**
 * copyright (c) 2017-2020 厦门风领科技有限公司
 * http://www.fenglinghudong.com/
 * 
 * 游戏数据中心
 * zengbinsi
 * 2018-10-05
 */

import { assetManager, ImageAsset, Texture2D, SpriteFrame, sys, Sprite, view } from "cc";
import { GGStore } from "./GGStore";

export class GameDataCenter {

    /** 最高等级 */
    public static set maxLevel(v) {
        GGStore.set('MaxLevel', v);
    }
    public static get maxLevel() {
        return GGStore.get('MaxLevel', 1);
    }

    private static _firstFree: boolean = true;
    /** 是否首次免费 */
    public static get isFirstFree() {
        GameDataCenter._firstFree = GGStore.get('BSFirstFree', true);
        return GameDataCenter._firstFree;
    }
    public static set isFirstFree(v) {
        GameDataCenter._firstFree = v;
        GGStore.set('BSFirstFree', v);
    }

    /**是否是低端机型 */
    public static isLowPerformance: boolean = false;
    /**是否是第一次进入 */
    public static isFirstEnter: boolean = true;


    /**游戏数据重置 */
    public static gameReset() {
        
    }

    /**是否调试模式 */
    public static IS_DEBUG: boolean = false;

    /**是否震动 */
    public static isEnaledShake: boolean = true;

    /**用户头像 */
    public static userHeadImg: string = null;
    /**用户名称 */
    public static userName: string = null;

    private static _isGetUserInfo: boolean = true;
    /**是否获得用户数据 */
    public static get isGetUserInfo(): boolean {
        GameDataCenter._isGetUserInfo = GGStore.get('_isGetUserInfo', false);
        return GameDataCenter._isGetUserInfo;
    }
    public static set isGetUserInfo(value: boolean) {
        GameDataCenter._isGetUserInfo = value;
        GGStore.set('_isGetUserInfo', value);
    }

    /**当前排名 */
    public static set curRank(v) {
        GGStore.set('CurRank', v);
    }
    public static get curRank() {
        return GGStore.get('CurRank', 0);
    }

    /**用户头像 */
    public static headImg = {};
    public static setHead(remoteUrl, sprite: Sprite) {
        if (!remoteUrl) return;
        if (GameDataCenter.headImg[remoteUrl]) {
            sprite.spriteFrame = GameDataCenter.headImg[remoteUrl];
        } else {
            assetManager.loadRemote(remoteUrl, { ext: '.png' }, (err, img: ImageAsset) => {
                if (err) {
                    console.log('加载失败', err);
                    return;
                }
                let texture = new Texture2D();
                texture.image = img;

                let newSpriteFrame = new SpriteFrame();
                newSpriteFrame.texture = texture;

                sprite.spriteFrame = newSpriteFrame;
                GameDataCenter.headImg[remoteUrl] = sprite.spriteFrame;
            });
        }
    }

    /** 开发域数据设置 */
    public static setUserCloudStorage(callback) {
        if (sys.platform == sys.Platform.WECHAT_GAME) {
            let KVData = [{
                key: 'MaxScore',
                value: "发送数据"
            },
                // {
                //     key: 'fishCount',
                //     value: GameDataCenter.passCount.toString()
                // },
            ]
            //设置微信托管数据
            wx.setUserCloudStorage({
                KVDataList: KVData,
                success: (res) => {
                    console.log('开发域数据设置成功', res)
                    callback && callback();
                },
                fail: (res) => {
                    console.log(res)
                },
                complete: (res) => {
                    console.log(res)
                }
            })
        }
    }

}

(window as any).GameDataCenter = GameDataCenter;